//package com.sghd.football.module.game.service.core.task;
//
//import com.sghd.common.socket.core.Message;
//import com.sghd.common.utils.json.JsonUtils;
//import com.sghd.football.module.data.service.DataService;
//import com.sghd.football.module.game.service.core.Desk;
//import com.sghd.football.module.game.service.core.Player;
//import com.sghd.football.module.game.service.core.Room;
//import com.sghd.football.module.game.service.model.PropType;
//import com.sghd.football.module.game.service.model.Result;
//import com.sghd.football.module.game.service.model.Status;
//import com.sghd.football.module.game.service.model.Window;
//import com.sghd.football.module.game.service.thread.Task;
//import com.sghd.football.net.PushReceiver;
//import com.sghd.football.net.SocketEnum;
//import com.sghd.football.utils.Callback;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.Arrays;
//import java.util.Date;
//
///**
// * \* Created with 王创辉.
// * \* Date: 2018/5/29
// * \* Time: 11:23
// * \* @desc: 结算Task
// */
//public class SettleTask implements Task {
//    private Logger logger = LoggerFactory.getLogger(getClass());
//    private Player player;
//    private Result result;
//    private long totalBet;
//    /**
//     * 数据服务
//     */
//    private DataService dataService;
//    /**
//     * 通知客户端，服务器结算完成，可以开始下一轮游戏
//     */
//    private PushReceiver pushReceiver;
//
//    public SettleTask(Player player, Result result, DataService dataService, long totalBet, PushReceiver pushReceiver) {
//        this.player = player;
//        this.result = result;
//        this.dataService = dataService;
//        this.pushReceiver = pushReceiver;
//        this.totalBet = totalBet;
//    }
//
//    @Override
//    public void run(Desk desk, Date now) {
//        Room room = desk.getRoom();
//        if (!result.isMgame() && !result.isCurrentFree()) {//只有普通旋转得分才抽成加入奖池
//            room.updatePool(totalBet);//更新奖池和反分池);
//        }
//        //结算(更新道具记录，第三方结算)
//        dataService.settle(player, result, new Callback<Result>() {
//            @Override
//            public void call(Result body) {
//                synchronized (player) {
//                    logger.debug("当前金币[{}]", player.getMondey(PropType.GOLD));
//                    logger.debug("变更金币[{}]", (body.getTotalFen() - body.getTotalBet()));
//                    player.change(PropType.GOLD, body.getTotalFen() - body.getTotalBet());
//                    logger.debug("结算后金币[{}]", player.getMondey(PropType.GOLD));
//                }
//                room.submit(desk, (ldesk, now) -> {
//                    if (result.isMgame()) {
//                        desk.removeStatus(Status.MINI_GAME);//小游戏完成(MINI_END), 移除状态
//                    } else {
//                        desk.removeStatus(Status.ROTATE);//旋转(结束)，移除状态
//                    }
//                    int type = 0;//默认是旋转结算
//                    if (result.isMgame()) {
//                        type = 1;//小游戏结算
//                    }
//                    //通知客户端:可以开始下一局游戏
//                    pushReceiver.push(Message.valueOf(SocketEnum.COMPLETE, "{\"type\":" + type + "}"), Arrays.asList(player.getPlayerId()).toArray());
//                });
//            }
//
//            @Override
//            public void onException(Exception ex, Result body) {
//                logger.error("玩家[{}]第三方服务结算失败[{}]", player.getPlayerId(), JsonUtils.object2String(body), new Object[]{ex});
//                dataService.settleError(player, body, ex);//保存结算失败记录
//                pushReceiver.dialog(Window.SETTLE_FAIL, Arrays.asList(player.getPlayerId()).toArray());
//            }
//        });
//    }
//
//    public Result getResult() {
//        return result;
//    }
//}
